package com.smsc.headend.connector.utils;

import cn.hutool.core.convert.Convert;
import com.smsc.headend.common.enums.DeviceType;
import com.smsc.headend.connector.bo.ConnectionInfo;
import com.smsc.headend.connector.service.asset.AssetService;
import com.smsc.headend.connector.service.impl.TerminalChannelServiceImpl;
import com.smsc.headend.module.asset.entity.ComEntity;
import com.smsc.headend.module.asset.entity.Meter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author wxh
 * @date 2021/6/29 16:15
 */
@Component
public class LogFileNameUtil {
    @Autowired
    AssetService assetService;
    @Autowired
    TerminalChannelServiceImpl terminalChannelService;

    public String getLogFileName(ComEntity comEntity) {
        if (comEntity == null) {
            return "anno-dev";
        }
        String logFileName = comEntity.getModuleSn();
        if (comEntity.getComDeviceType().equals(DeviceType.GPRS.getId())) {
            Meter meter = assetService.getMeterById(comEntity.getDeviceId());
            if (meter != null) {
                logFileName = meter.getMfgSn();
            }
        }
        return logFileName;
    }

    public String getConnectInfoLogFileName(ConnectionInfo connectionInfo) {
        if ("-1".equalsIgnoreCase(Convert.toStr(connectionInfo.getComId()))) {
            return "annoy-device";
        }
        String logFileName = connectionInfo.getModuleSn();
        ComEntity comEntity = terminalChannelService.getTerminalById(connectionInfo.getComId());
        if (comEntity != null && comEntity.getComDeviceType().equals(DeviceType.GPRS.getId())) {
            Meter meter = assetService.getMeterById(comEntity.getDeviceId());
            if (meter != null) {
                logFileName = meter.getMfgSn();
            }
        }
        return logFileName;
    }

}
